﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.Video;
using Assets.Scripts.TDuResource;
using RenderHeads.Media.AVProVideo;
using Assets.Scripts.TDu3DWork;

namespace Assets.Scripts.TDuVRUI
{
    class TDuVideoDashboard
    {
        private MediaPlayer videoPlayer;
        private GameObject VideoPanel;

        public void create(TDuSceneBaseWorker baseWorker, VariableNode curVarNode, GameObject parent)
        {
            VideoPanel = parent.transform.Find("VideoPanel").gameObject;
            VideoPanel.SetActive(true);
            parent.SetActive(true);

            GameObject videoRaw = VideoPanel.transform.Find("VideoWnd").gameObject;
            videoPlayer = videoRaw.GetComponent<MediaPlayer>();

            //#if UNITY_EDITOR
            //            string filepath = Application.streamingAssetsPath + "/Resources/Scene/aaa.mov";
            //#elif UNITY_ANDROID
            //	string filepath = "jar:file://" + Application.dataPath + "!/assets/" +  "Resources/Scene/aaa.mov";
            //#endif
            string filepath = Application.streamingAssetsPath + "/Resources/Scene/测试视频.mov";
            //string filepath = "http://cdn.youzhiyou.net/prod/0.08515545526037638如何叠衣服.mov";
            videoPlayer.m_VideoPath = filepath;
            videoPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, filepath, true);
            Debug.Log("Android Path==========="+ filepath);
            videoPlayer.Control.Play();
            //videoPlayer.url = filepath;


            GameObject Ok_uiButton = VideoPanel.transform.Find("Ok_uiButton").gameObject;

            Ok_uiButton.GetComponent<Button>().onClick.AddListener(
                        () =>
                        {
                            TDuSceneWorker sceneworker = (TDuSceneWorker)baseWorker;
                            sceneworker.updateMsgVar("", true);
                            Debug.Log("点击视频关闭");
                            VideoPanel.SetActive(false);
                            parent.SetActive(false);
                        }
                    );
            //RawImage生成VideoWnd
        }
        //public void create(VariableNode curVarNode, GameObject parent)
        //{
        //    //RawImage生成VideoWnd
        //    DefaultControls.Resources video_uiResources = new DefaultControls.Resources();
        //    GameObject uiPanel = DefaultControls.CreatePanel(video_uiResources);
        //    uiPanel.name = "VideoPanel";
        //    uiPanel.transform.SetParent(parent.transform, false);
        //    uiPanel.GetComponent<RectTransform>().localScale = new Vector3(0.5f, 0.5f, 0.5f);
        //    uiPanel.GetComponent<Image>().color = new Color(0, 0, 0, 0);
        //    uiPanel.SetActive(false);

        //    DefaultControls.Resources rawImage_uiResources = new DefaultControls.Resources();
        //    GameObject videoRaw = DefaultControls.CreateRawImage(rawImage_uiResources);
        //    videoRaw.transform.SetParent(uiPanel.transform, false);
        //    videoRaw.GetComponent<RectTransform>().sizeDelta = new Vector2(800F, 400F);
        //    videoRaw.name = "VideoWnd";
        //    videoPlayer = videoRaw.AddComponent<VideoPlayer>();
        //    videoPlayer.url = Application.streamingAssetsPath + "/Resources/Scene/洗手戴口罩.mov";
        //    RenderTexture render = new RenderTexture(256, 256, 0);
        //    videoPlayer.targetTexture = render;
        //    videoRaw.GetComponent<RawImage>().texture = render;

        //    DefaultControls.Resources Ok_uiResources = new DefaultControls.Resources();
        //    GameObject Ok_uiButton = DefaultControls.CreateButton(Ok_uiResources);
        //    Ok_uiButton.transform.SetParent(uiPanel.transform, false);
        //    Ok_uiButton.GetComponent<RectTransform>().sizeDelta = new Vector2(200F, 60F);
        //    Ok_uiButton.GetComponent<RectTransform>().anchorMin = new Vector2(0.5F, 0.5F);
        //    Ok_uiButton.GetComponent<RectTransform>().anchorMax = new Vector2(0.5F, 0.5F);
        //    Ok_uiButton.transform.localPosition = new Vector3(0, -240, 0);
        //    Ok_uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().color = Color.black;
        //    Ok_uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().text = "确定";
        //    Ok_uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().fontSize = 40;

        //    Ok_uiButton.GetComponent<Button>().onClick.AddListener(
        //                () =>
        //                {
        //                    {
        //                        Debug.Log("点击视频关闭");
        //                        uiPanel.SetActive(false);
        //                    }
        //                }
        //            );
        //}
    }
}
